<?php
/**
 * 钻石投票模块定义
 *
 * @author 天涯织梦
 * @url http://bbs.we7.cc/
 */
defined('IN_IA') or exit('Access Denied');

class Tyzm_diamondvoteModule extends WeModule {
	public $table_reply = 'tyzm_diamondvote_reply';
	public function fieldsFormDisplay($rid = 0) {
		//要嵌入规则编辑页的自定义内容，这里 $rid 为对应的规则编号，新增时为 0
				
		$baiduapikey=$this->module['config']['baiduapikey'];

		if(empty($rid)){
			$reply = array(
				'title'=> '年度投票，震惊联合国，男神女神，萌宝帅宠，你也来一发！',
				'thumb' => "../addons/tyzm_diamondvote/template/static/images/topimg.jpg",
				'description' => "全民投票的时代，你怎么能错过，邀请好友一起来帮你吧。",
				'starttime' => time(),
				'endtime' => time() + 10 * 84400,
				'topimg' => "../addons/tyzm_diamondvote/template/static/images/topimg.jpg",
				'bgcolor' => "#fff",
				'area' => "",
				'followguide'=>"关注公众号后，点击菜单或回复“投票”即可参加投票。",
				'exchange'=>3,
				'jdexchange'=>3,
				'diamondmodel'=>0,
				'rankingnum'=>20,
				'everyoneuser'=>2,
				'dailyvote'=>6,
				'everyonevote'=>50,			
				'status' => 1,

				
			);
			
		}else{
			$reply = pdo_fetch("SELECT * FROM ".tablename($this->table_reply)." WHERE rid = :rid ORDER BY `id` DESC", array(':rid' => $rid));
			$reply=array_merge ($reply,unserialize($reply['config']));
			
		}
		include $this->template('form');
		
	}

	public function fieldsFormValidate($rid = 0) {
		//规则编辑保存时，要进行的数据验证，返回空串表示验证无误，返回其他字符串将呈现为错误提示。这里 $rid 为对应的规则编号，新增时为 0
		return '';
	}

	public function fieldsFormSubmit($rid) {
		//规则验证无误保存入库时执行，这里应该进行自定义字段的保存。这里 $rid 为对应的规则编号
		global $_W,$_GPC;
		$id = intval($_GPC['reply_id']);
		/*
		`isfollow`  '是否需要关注，0不关注，1参加关注，2投票参加关注',
		`unsubscribe` '取消订阅减票',
		`diamondmodel` '是否钻石模式',
		`exchange` '一颗砖石兑换几票',
		`rankingnum`'显示多少排行榜',
		`everyoneuser`每人每用户最多投多少',
		`dailyvote`'每日每人最多投票',
		`everyonevote`总共能投票次数',
		`ischecked`是否需要审核',
		*/
		$config=iserializer(array('isfollow'=>$_GPC['isfollow'],'unsubscribe'=>$_GPC['unsubscribe'],'diamondmodel'=>$_GPC['diamondmodel'],'exchange'=>$_GPC['exchange'],'jdexchange'=>$_GPC['jdexchange'],'rankingnum'=>$_GPC['rankingnum'],'everyoneuser'=>$_GPC['everyoneuser'],'dailyvote'=>$_GPC['dailyvote'],'everyonevote'=>$_GPC['everyonevote'],'ischecked'=>$_GPC['ischecked'])); 

		$insert = array(
			'rid' => $rid,
			'uniacid' => $_W['uniacid'],
			'title' => $_GPC['title'],
			'thumb' => $_GPC['thumb'],
			'description' => $_GPC['description'],
			'starttime' => strtotime($_GPC['time']['start']),
			'endtime' => strtotime($_GPC['time']['end']),
			'topimg' => $_GPC['topimg'],
			'bgcolor' => $_GPC['bgcolor'],
			'eventrule' => htmlspecialchars_decode($_GPC['eventrule']),
			'awardmsg' => htmlspecialchars_decode($_GPC['awardmsg']),
			'prizemsg' => htmlspecialchars_decode($_GPC['prizemsg']),
			'infomsg' => htmlspecialchars_decode($_GPC['infomsg']),
			'endintro'=> htmlspecialchars_decode($_GPC['endintro']),
			'config'=>$config,
			'usepwd' => $_GPC['usepwd'],
			'area' => $_GPC['area'],
			'followguide'=> $_GPC['followguide'],
			'sharetitle' => $_GPC['sharetitle'],
			'shareimg' => $_GPC['shareimg'],
			'sharedesc' => $_GPC['sharedesc'],
			'status' => $_GPC['status'],
			'createtime' => time(),	
	    );
		if (empty($id)) {
			pdo_insert($this->table_reply, $insert);
		}else{
			unset($insert['createtime']);
			pdo_update($this->table_reply, $insert, array('id' => $id));
		}
	}

	public function ruleDeleted($rid) {
		//删除规则时调用，这里 $rid 为对应的规则编号
		$replies = pdo_fetchall("SELECT id  FROM ".tablename($this->table_reply)." WHERE rid = '$rid'");
		$deleteid = array();
		if (!empty($replies)) {
			foreach ($replies as $index => $row) {
				$deleteid[] = $row['id'];
			}
		}
		pdo_delete($this->table_reply, "id IN ('".implode("','", $deleteid)."')");
	}

	public function settingsDisplay($settings) {
		global $_W, $_GPC;
		//点击模块设置时将调用此方法呈现模块设置页面，$settings 为模块设置参数, 结构为数组。这个参数系统针对不同公众账号独立保存。
		//在此呈现页面中自行处理post请求并保存设置参数（通过使用$this->saveSettings()来实现）
		if(checksubmit()) {
			//字段验证, 并获得正确的数据$dat
			$dat=array(
			    'baiduapikey'=>$_GPC['baiduapikey'],
			);
			$this->saveSettings($dat);
			message('保存成功', 'refresh');
		}
		//这里来展示设置项表单
		include $this->template('setting');
	}

}

